package com.cskaoyan.service;

import com.cskaoyan.bean.bo.IssueCreateBO;
import com.cskaoyan.bean.param.BaseParam;

import com.cskaoyan.bean.vo.IssueVO.Issue;
import com.cskaoyan.bean.vo.IssueVO.IssueData;
import com.cskaoyan.bean.vo.IssueVO.IssueUpdateVO;
import com.cskaoyan.mapper.IssueMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueData query(BaseParam param, String question) {
        PageHelper.startPage(param.getPage(), param.getLimit());
        // 使用的userMapper中并没有提供page和limit
        List<Issue> issues = issueMapper.queryAll(param.getSort(), param.getOrder(),question);
        // 会去获得一些分页信息
        // 上面的查询对应的PageInfo → 把原始查询结果放入到构造方法里
        PageInfo pageInfo = new PageInfo(issues);

        // total是总的数据量 → 是否等于Orders.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return IssueData.data(total,pages,param.getLimit(), param.getPage(), issues);
    }

    @Override
    public int insertIssueGoods(IssueCreateBO issueCreateBO) {
        issueMapper.insert(issueCreateBO,new Date());
        Integer id =  issueCreateBO.getId();
        System.out.println("插入成功，id为：" + id);
        return id;
    }

    @Override
    public int updateIssueGoods(IssueCreateBO issueCreateBO) {
        int i = issueMapper.update(issueCreateBO,new Date());
        return i;
    }


    @Override
    public IssueUpdateVO selectIssueGoods(IssueCreateBO issueCreateBO) {
        IssueUpdateVO    issueUpdateVO   = issueMapper.selectIssue(issueCreateBO);

        return null;
    }

    @Override
    public void update4delete(IssueCreateBO issueCreateBO) {
      issueMapper.updateForDelete(issueCreateBO);
    }


}
